//
//  OMData.m
//  Weibbo
//
//  Created by ifuninfo on 13-6-9.
//  Copyright (c) 2013年 Omnivores. All rights reserved.
//

#import "OMData.h"
#import "SynthesizeSingleton.h"
#import "JSONKit.h"
#define  K_INITDATA @"INITDATA"
#define  K_MUTABLEDATA @"MUTABLEDATA"

#define  K_DRAFT @"DRAFT"
NSString * const LOCAL_DOCUMENT_FILE_NAME = @"mutabledata.txt";
NSString * const RESOURCE_FILE_NAME = @"initdata.txt";

@implementation OMData
@synthesize data = _data;
SYNTHESIZE_SINGLETON_FOR_CLASS(OMData)
- (id)init
{
    self = [super init];
    if (self != nil) {
        
        _data = [[NSMutableDictionary alloc] init];
        NSString *resourceFileName = UIResourceFile(RESOURCE_FILE_NAME);
        NSString  *resourceString = [NSString stringWithContentsOfFile:resourceFileName  encoding:NSUTF8StringEncoding error:nil];
        NSMutableDictionary *resourceDictionary = [resourceString mutableObjectFromJSONString];
        [_data setObject:resourceDictionary forKey:K_INITDATA];
        
        
        NSString *localFileName = UIDocumentFile(LOCAL_DOCUMENT_FILE_NAME);
        NSMutableDictionary *localDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:localFileName];
        if (localDictionary == nil)
        {
            localDictionary = [NSMutableDictionary dictionary];
            NSMutableArray  *draft = [resourceDictionary objectForKey:@"draft"];
            [localDictionary setObject:draft forKey:K_DRAFT];
        }
        [_data setObject:localDictionary forKey:K_MUTABLEDATA];
        
        

    }
    return self;
}
- (NSMutableArray *)draftList
{
    return [[_data objectForKey:K_MUTABLEDATA] objectForKey:K_DRAFT];
}
+(OMData *)sharedData
{
    return [OMData sharedOMData];
}
- (void)save
{
    NSDictionary *mutableData = [_data objectForKey:K_MUTABLEDATA];
    [mutableData writeToFile:UIDocumentFile(LOCAL_DOCUMENT_FILE_NAME) atomically:YES];
}
@end
